package com.teradata.tool.util;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class POIUtils {

	public static List<String> getSheetName(String path) {
		if (path == "") {
			return null;
		}
		File f = new File(path);
		if (f.exists()) {
			try {
				InputStream fis = new FileInputStream(f);
				POIFSFileSystem poifs = new POIFSFileSystem(fis);
				HSSFWorkbook wb = new HSSFWorkbook(poifs);

				List<String> list = new ArrayList<String>();
				for (int i = 0; i < wb.getNumberOfSheets(); i++) {
					list.add(wb.getSheetName(i));
				}
				return list;
			} catch (Exception ee) {
				ee.printStackTrace();
			}// ��ȡ��XLS·��
		}

		return null;
	}

	public static List<String> getRows(int rowNum, HSSFSheet sheet) {
		HSSFRow row = sheet.getRow(rowNum);
		int colcount = row.getLastCellNum();// �ж��ж�����

		List<String> list = new ArrayList<String>();
		for (int i = 0; i < colcount; i++) {
			HSSFCell cell = row.getCell(i);
			cell.setCellType(HSSFCell.CELL_TYPE_STRING);
			list.add(cell.getStringCellValue());
		}
		return list;
	}

	public static final String OUTPUT_CHARSET = "GBK";
	public static final String delimeter = "$|";
	public static void sheet2txt(String outPath, HSSFSheet sheet,
			ArrayList<Integer> colOrderList, boolean isOmitHead) {
		int i = 0;
		if (isOmitHead) {
			i = 1;
		}
		int rowCount = sheet.getLastRowNum();
		PrintStream output = null;
		try {
			output = new PrintStream(outPath, OUTPUT_CHARSET);

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (; i <= rowCount; i++) {
			String line = "";
			for (int j : colOrderList) {
				HSSFCell cell = sheet.getRow(i).getCell(j-1);
				if(cell==null){line = line+ delimeter;}else{
					cell.setCellType(HSSFCell.CELL_TYPE_STRING);
					line = line+(cell.getStringCellValue()+delimeter);
				}
			}

			output.println(line.substring(0,line.length()-2));
		}
		output.close();

	}

	public static HSSFSheet getSheetBySheetName(String path, String sheetName) {
		if (path != "") {
			File f = new File(path);
			if (f.exists()) {
				try {
					InputStream fis = new FileInputStream(f);
					POIFSFileSystem poifs = new POIFSFileSystem(fis);
					HSSFWorkbook wb = new HSSFWorkbook(poifs);
					for (int i = 0; i < wb.getNumberOfSheets(); i++) {
						if (wb.getSheetName(i).equals(sheetName)) {
							return wb.getSheetAt(i);
						}
					}
				} catch (Exception ee) {
					ee.printStackTrace();
				}// ��ȡ��XLS·��
			}
		}
		return null;
	}
}
